package com.pth.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pth.dao.LogDAO;
import com.pth.model.Jianjie;
import com.pth.model.Log;

public class LogDAOImpl implements LogDAO {

	private HibernateTemplate hibernateTemplate;
	
	
	public void add(Log log) {

		hibernateTemplate.save(log);
		
	}

	
	@SuppressWarnings("unchecked")
	public List<Log> getAllLogs(final int pagesize,final int offset) {


		final String hqlString="from Log log order by log.visitime desc";
		
		List<Log> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hqlString).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
	
	}

	public Log getLogByid(int id) {


		return hibernateTemplate.get(Log.class, id);
		
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}


	public int getAllNum() {

		String hql="from Log l";
		
		return hibernateTemplate.find(hql).size();
		
		
	}


	@SuppressWarnings("unchecked")
	public List<Log> getLogsBytime(final int pagesize,final int offset,String time) {


		final String hqlString="from Log log where log.visitime like '%"+time+"%' order by log.visitime desc";
		
		List<Log> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hqlString).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
		
	}


	public int getNumBytime(String time) {

		String hqlString="from Log log where log.visitime like '%"+time+"%' order by log.visitime desc";
		
		return hibernateTemplate.find(hqlString).size();
		
	}

}
